package com.quectel.emp.controller.scheduling;

import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.business.common.scheduling.SchedulingLeaveBusiness;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.module.scheduling.dto.SchedulingLeaveBalanceDto;
import com.quectel.core.module.scheduling.dto.SchedulingLeaveConfigDto;
import com.quectel.core.module.scheduling.service.SchedulingLeaveBalanceService;
import com.quectel.core.module.scheduling.service.SchedulingLeaveConfigService;
import com.quectel.core.module.scheduling.service.SchedulingLeavePersonBindingService;
import com.quectel.emp.controller.BaseController;
import com.quectel.emp.util.SessionHolder;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/7/11 13:06
 * @description 考勤假期列表接口
 */
@RestController
@RequestMapping("schedulingLeaveBalance")
@Tag(name = "SchedulingLeaveBalanceController", description = "考勤假期余额相关列表")
public class SchedulingLeaveBalanceController extends BaseController {


    @DubboReference
    private SchedulingLeaveConfigService schedulingLeaveConfigService;
    @DubboReference
    private SchedulingLeavePersonBindingService schedulingLeavePersonBindingService;
    @DubboReference
    private SchedulingLeaveBalanceService schedulingLeaveBalanceService;


    @Autowired
    private SchedulingLeaveBusiness schedulingLeaveBusiness;



    /**
     * 有余额的假期类型查询
     *
     * @return
     */
    @GetMapping("list")
    @Operation(summary = "有余额的假期类型查询")
    public Response<Page<SchedulingLeaveBalanceDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize,
            @Parameter(description = "假期id") @RequestParam Long leaveId
    ) {
        SessionHolder.checkEmp();

        SchedulingLeaveConfigDto schedulingLeaveConfigDto = schedulingLeaveConfigService.selectCacheById(leaveId);

        Response response = schedulingLeaveBusiness.checkLeaveConfig(schedulingLeaveConfigDto);
        if (!response.isSuccess()) {
            return Response.error(response.getMessage());
        }
        //如果不是限额的 则没有余额
        if (SystemConstants.NO.equals(schedulingLeaveConfigDto.getLimitFlag())) {
            return Response.error("该类型假期没有余额");
        }

        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        request.put(LambdaUtil.getFieldName(SchedulingLeaveBalanceDto::getPersonId), SessionHolder.getEmp().getId());
        request.put(LambdaUtil.getFieldName(SchedulingLeaveBalanceDto::getLeaveId), leaveId);

        int total = schedulingLeaveBalanceService.queryTotal(request.getParams());
        List<SchedulingLeaveBalanceDto> list = new ArrayList<>();
        if (total > 0) {
            list = schedulingLeaveBalanceService.queryList(request.getParams());
        }
        Page<SchedulingLeaveBalanceDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);

        return Response.<Page<SchedulingLeaveBalanceDto>>ok().wrap(page);
    }

}
